package com.wwtech.epp.ui.activity.work.presenter;
import android.support.annotation.NonNull;

import com.socks.library.KLog;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.entity.BaseBack;
import com.wwtech.epp.entity.Daily;
import com.wwtech.epp.ui.activity.work.contract.DailyContract;
import com.wwtech.epp.ui.activity.work.DailyFragment;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: presenter of DailyFragment
 * @date 2017/12/07 17:36:20
 */
public class DailyPresenter implements DailyContract.DailyContractPresenter{

    HttpAPIWrapper httpAPIWrapper;
    private final DailyContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private DailyFragment mFragment;

    @Inject
    public DailyPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull DailyContract.View view, DailyFragment fragment) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mFragment = fragment;
    }
    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
             mCompositeDisposable.dispose();
        }
    }

    @Override
    public void getDailyListFromServer(String type, int pageNum, int pageSize) {
        Map<String, String> map = new HashMap<>();
        map.put("pageSize", pageSize + "");
        map.put("pageNum", pageNum + "");
        map.put("userId", ContainValue.userLogin.getData().getUserId() + "");
        if ("我发出的".equals(type)) {
            mView.showProgressDialog();
            Disposable disposable = httpAPIWrapper.getMyReport(ContainValue.userLogin.getData().getUserId() + "", map)
                    .subscribe(new Consumer<Daily>() {
                        @Override
                        public void accept(Daily daily) throws Exception {
                            //isSuccesse
                            KLog.i("onSuccesse");
                            mView.closeProgressDialog();
                            mView.setListData(daily);
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            //onError
                            KLog.i("onError");
                            throwable.printStackTrace();
                        }
                    }, new Action() {
                        @Override
                        public void run() throws Exception {
                            //onComplete
                            KLog.i("onComplete");
                        }
                    });
            mCompositeDisposable.add(disposable);
        } else if ("我收到的".equals(type)) {
            mView.showProgressDialog();
            Disposable disposable = httpAPIWrapper.getReceiveReport(ContainValue.userLogin.getData().getUserId() + "", map)
                    .subscribe(new Consumer<Daily>() {
                        @Override
                        public void accept(Daily daily) throws Exception {
                            //isSuccesse
                            KLog.i("onSuccesse");
                            mView.closeProgressDialog();
                            mView.setListData(daily);
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            //onError
                            KLog.i("onError");
                            throwable.printStackTrace();
                        }
                    }, new Action() {
                        @Override
                        public void run() throws Exception {
                            //onComplete
                            KLog.i("onComplete");
                        }
                    });
            mCompositeDisposable.add(disposable);
        }
    }

}